package com.sample.springbatch.job.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.listener.StepListenerSupport;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 对作业步骤的监听实现类
 * Created by zwg on 2017/8/24.
 */
@Component
public class MyStepListener<T, S> extends StepListenerSupport {

    private static final Logger logger = LoggerFactory.getLogger(MyStepListener.class);

    @Autowired
    private MapExecutionTracker mapExecutionTracker;

    @Override
    public void beforeStep(StepExecution stepExecution) {
        super.beforeStep(stepExecution);
        mapExecutionTracker.saveStepExecution(stepExecution);
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        mapExecutionTracker.saveStepExecution(stepExecution);
        return super.afterStep(stepExecution);
    }

    @Override
    public void onSkipInRead(Throwable t) {
        logger.error("read skip error",t);
    }

    @Override
    public void onReadError(Exception ex) {
        super.onReadError(ex);
        logger.error("",ex);
    }


    @Override
    public void onWriteError(Exception exception, List items) {
        super.onWriteError(exception, items);
        logger.error("",exception);
    }

    @Override
    public void onProcessError(Object item, Exception e) {
        super.onProcessError(item, e);
        logger.error("",e);
    }

    @Override
    public void afterChunkError(ChunkContext context) {
        super.afterChunkError(context);

        logger.error("",context.toString());

    }

}
